/*
 * -------------------------------------------------------------------------------------------------------*
 * SBS Priceboard V2.0
 * Copyright (C) 2008 Unicom Vietnam JSC
 * -------------------------------------------------------------------------------------------------------* 
 * HeadURL: http://192.168.1.4/svn/priceboard/trunk/SBS_PriceboardV2/front-end/end-user/enduser_priceboard/
 * LastChangeBy:
 * LastChangeDate:
 */
package com.unicom.priceboard.util;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import com.unicom.priceboard.thread.FroomThread;
import com.unicom.priceboard.thread.HaStockThread;
import com.unicom.priceboard.thread.LeThread;
import com.unicom.priceboard.thread.MarketStatusThread;
import com.unicom.priceboard.thread.PutAdThread;
import com.unicom.priceboard.thread.PutExecThread;
import com.unicom.priceboard.thread.SecurityThread;
import com.unicom.priceboard.thread.TimerThread;
import com.unicom.priceboard.thread.TotalMarketThread;

/**
 * Creation date: Jul 18, 2008
 * @author quan
 */
public class PriceboardThreatInit extends HttpServlet {
	private static final long serialVersionUID = 3512702269593885114L;
	
	private static boolean timerInit = false;
	Thread timerThread = null;
	
	private static boolean hastcInit = false;
	Thread hastcThread = null;
	
	private static boolean hoseInit = false;
	Thread hoseIndex= null;
	Thread hosestatus= null;
	Thread stocks = null;
	Thread hosePutAd=null;
	Thread hoseStock=null;
	Thread putExec =null;
	Thread hoseLe	=null;
	Thread hoseFroom=null;
	
	@Override
	public synchronized void init(ServletConfig config) throws ServletException{
		//set datapath
//		DataPath.basePath = "E:/priceboard/";
//		DataPath.fRoomPath = "FROOM.DAT";
//		DataPath.oldRoom = "E:/priceboard/FROOMOLD.DAT";
//		DataPath.zipFilePath = "E:/priceboard/STS_Info_Public.zip";
//		DataPath.leSrcPath = "LE.DAT";
//		DataPath.securityPath = "SECURITY.DAT";
//		DataPath.putAdSrcPath = "PUT_AD.DAT";
//		DataPath.putExecSrcPath = "PUT_EXEC.DAT";
//		DataPath.openPricexml = "E:/priceboard/OpenPrice.xml";
//		DataPath.hoseWait = "5";
//		DataPath.hastcWait = "5";
//		DataPath.totalMarketSrcPath = "TOTALMKT.DAT";
//		DataPath.sessionPath = "E:/priceboard/session.txt";
//		DataPath.marketStaSrctPath = "MARKET_STAT.DAT";
		
//		MessageSourceUtil cfg = new MessageSourceUtil();
//		
//		DataPath.basePath = cfg.getMessageValue("priceboard.file.hose.parentFolder");
//		DataPath.fRoomPath = cfg.getMessageValue("priceboard.file.hose.froom.name");
//		DataPath.oldRoom = cfg.getMessageValue("priceboard.hose.FroomOld");
//		DataPath.zipFilePath = cfg.getMessageValue("priceboard.file.hastc.path");
//		DataPath.leSrcPath = cfg.getMessageValue("priceboard.file.hose.le.name");
//		DataPath.securityPath = cfg.getMessageValue("priceboard.file.hose.security.name");
//		DataPath.putAdSrcPath = cfg.getMessageValue("priceboard.file.hose.putAd.name");
//		DataPath.putExecSrcPath = cfg.getMessageValue("priceboard.file.hose.putExec.name");
//		DataPath.openPricexml = cfg.getMessageValue("priceboard.file.hose.openPrice");
//		DataPath.hoseWait = cfg.getMessageValue("priceboard.hose.wait");
//		DataPath.hastcWait = cfg.getMessageValue("priceboard.hastc.wait");
//		DataPath.totalMarketSrcPath = cfg.getMessageValue("priceboard.file.hose.totalmkt.name");
//		DataPath.sessionPath = cfg.getMessageValue("priceboard.hose.session");
//		DataPath.marketStaSrctPath = cfg.getMessageValue("priceboard.file.hose.marketStat.name");
//		
//		DataPath.hastcTimeStart = cfg.getMessageValue("priceboard.hastc.time.start");
//		DataPath.hastcTimeEnd = cfg.getMessageValue("priceboard.hastc.time.end");
//		DataPath.hoseTimeStart = cfg.getMessageValue("priceboard.hose.time.start");
//		DataPath.hoseTimeEnd = cfg.getMessageValue("priceboard.hose.time.end");
		super.init(config);
		DataPath.basePath = config.getInitParameter("basePath");
		DataPath.fRoomPath = config.getInitParameter("fRoomPath");
		DataPath.oldRoom = config.getInitParameter("oldRoom");
		DataPath.zipFilePath = config.getInitParameter("zipFilePath");
		DataPath.leSrcPath = config.getInitParameter("leSrcPath");
		DataPath.securityPath = config.getInitParameter("securityPath");
		DataPath.putAdSrcPath = config.getInitParameter("putAdSrcPath");
		DataPath.putExecSrcPath = config.getInitParameter("putExecSrcPath");
		DataPath.openPricexml = config.getInitParameter("openPricexml");
		DataPath.hoseWait = config.getInitParameter("hoseWait");
		DataPath.hastcWait = config.getInitParameter("hastcWait");
		DataPath.totalMarketSrcPath = config.getInitParameter("totalMarketSrcPath");
		DataPath.sessionPath = config.getInitParameter("sessionPath");
		DataPath.marketStaSrctPath = config.getInitParameter("marketStaSrctPath");
		DataPath.hoseTimeStart = config.getInitParameter("hoseTimeStart");
		DataPath.hoseTimeEnd = config.getInitParameter("hoseTimeEnd");
		DataPath.hastcTimeStart = config.getInitParameter("hastcTimeStart");
		DataPath.hastcTimeEnd = config.getInitParameter("hastcTimeEnd");
		DataPath.dataBaseUpdateTime = config.getInitParameter("dataBaseUpdateTime");
		
		if(Boolean.parseBoolean(config.getInitParameter("printDatapath"))){
			System.out.println(DataPath.basePath);
			System.out.println(DataPath.fRoomPath);
			System.out.println(DataPath.oldRoom);
			System.out.println(DataPath.zipFilePath);
			System.out.println(DataPath.leSrcPath);
			System.out.println(DataPath.securityPath);
			System.out.println(DataPath.putAdSrcPath);
			System.out.println(DataPath.putExecSrcPath);
			System.out.println(DataPath.openPricexml);
			System.out.println(DataPath.hoseWait);
			System.out.println(DataPath.hastcWait);
			System.out.println(DataPath.totalMarketSrcPath);
			System.out.println(DataPath.sessionPath);
			System.out.println(DataPath.marketStaSrctPath);
			System.out.println(DataPath.hoseTimeStart);
			System.out.println(DataPath.hoseTimeEnd);
			System.out.println(DataPath.hastcTimeStart);
			System.out.println(DataPath.hastcTimeEnd);
			System.out.println(DataPath.dataBaseUpdateTime);
		}
		
		
		//start timer threat
		if(!timerInit) {
			timerInit = true;
			
			TimerThread tThread = new TimerThread();
			timerThread = new Thread(tThread);
			timerThread.start();
		}
		
		//start hastc threat
		if(!hastcInit) {
			hastcInit = true;
			HaStockThread haThread = new HaStockThread();
			hastcThread = new Thread(haThread);
			hastcThread.start();
		}
		
		//start hose threat
		if(!hoseInit) {
			hoseInit = true;
			
			TotalMarketThread hoIndexThread = new TotalMarketThread();
			MarketStatusThread marketstatus = new MarketStatusThread();
			SecurityThread stockThread = new SecurityThread();
			PutAdThread putAdThread = new PutAdThread();
			SecurityThread security = new SecurityThread();
			PutExecThread putExecThread = new PutExecThread();
			LeThread leReader = new LeThread();
			FroomThread froomReader = new FroomThread();
			
			hoseStock = new Thread(security);
			hoseIndex = new Thread(hoIndexThread);
			hosestatus = new Thread(marketstatus);
			putExec = new Thread(putExecThread);
			hoseLe = new Thread(leReader);
			hoseFroom = new Thread(froomReader);
			/** **********put through deals *********** */
			hosePutAd = new Thread(putAdThread);
			/** **********the end*********** */
			
			
			stocks = new Thread(stockThread);
			hosestatus.start();
			hoseIndex.start();
			
			System.out.println(hoseIndex.isAlive());
			
			hoseLe.start();
			hoseFroom.start();
			try{
				if(FroomThread.froomFinish==false&&LeThread.leFlag==false){
					Thread.sleep(2000);
				}
			}catch(InterruptedException e){
				System.out.println("thread is error for read Froom or Le"+e.getMessage());
			}
			stocks.start();
			try{
				if(SecurityThread.securityFlag==false){
					Thread.sleep(2000);
				}
			}catch(InterruptedException e){
				System.out.println(" thread sleep Error: " +e.getMessage());
			}
			hosePutAd.start();
			putExec.start();
			
		
			
		}
		
		
	}
	
	@Override
	public void destroy() {
		//destroy hastc threat
		if(hastcThread!=null) {
			hastcThread.interrupt();
			hastcThread = null;
		}
		
		//destroy hose threat
		if(hoseIndex!=null) {
			hoseIndex.interrupt();
			hoseIndex = null;
		}
		if(hosestatus!=null){
			hosestatus.interrupt();
			hosestatus=null;
		}

		
		if(hosePutAd!=null){
			hosePutAd.interrupt();
			hosePutAd=null;
		}

		if(stocks!=null) {
			stocks.interrupt();
			stocks = null;
		}
		if(putExec!=null){
			putExec.interrupt();
			putExec=null;
		}
		if(hoseFroom!=null){
			hoseFroom.interrupt();
			hoseFroom=null;
		}
		if(hoseLe!=null){
			hoseLe.interrupt();
			hoseLe=null;
		}
		
		//destroy timer threat
		if(timerThread!=null) {
			timerThread.interrupt();
			timerThread = null;
		}
		super.destroy();
	}
}

